package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.AddVehicleFrame;

/**
 * 3 avr. 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ApplyModificationFleet.java
 */
public class ApplyModificationFleet extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = -2959786453066426500L;

    /**
     * Default constructor.
     */
    public ApplyModificationFleet() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.APPLY_MODIFICATIONS));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.APPLY_MODIFICATIONS));
        this.putValue(SMALL_ICON, ImagesDatabase.EDIT);
    }

    /**
     * Changes the data of the selected vehicle and replace it by the new ones (
     * enter in the input areas)
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        AddVehicleFrame.getInstance().getAdd().setVisible(true);
        AddVehicleFrame.getInstance().getApplyModification().setVisible(false);
        String capacity = AddVehicleFrame.getInstance().getCapacity().getText();
        String maxDriveduration = AddVehicleFrame.getInstance()
                .getMaxDriveDuration().getText();
        PUDStation startingPoint = (PUDStation) AddVehicleFrame.getInstance()
                .getStartingPoint().getSelectedItem();
        PUDStation endingPoint = (PUDStation) AddVehicleFrame.getInstance()
                .getEndingPoint().getSelectedItem();
        if (OpenFrameAddVehicle.checkInputUser(capacity, maxDriveduration,
                startingPoint, endingPoint)) {
            Vehicle v = (Vehicle) AddVehicleFrame.getInstance()
                    .getFleetInformation().getSelectedValue();
            v.setCapacity(Integer.parseInt(capacity));
            v.setMaxDriveDuration(Integer.parseInt(maxDriveduration));
            v.setStartingPoint(startingPoint);
            v.setEndingPoint(endingPoint);
            Fleet.getInstance().setFleet();
            AddVehicleFrame.getInstance().getApplyModification()
                    .setVisible(false);
            AddVehicleFrame.getInstance().getAdd().setVisible(true);
        }
        else {
            AddVehicleFrame.getInstance().getApplyModification()
                    .setVisible(true);
            AddVehicleFrame.getInstance().getAdd().setVisible(false);
        }
    }
}
